package com.wwj.send.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wwj.send.constant.ConstantFinal;
import com.wwj.send.dao.SendDao;
import com.wwj.send.entitiy.SendInformationEntity;
import com.wwj.send.service.SendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class SendServiceImpl extends ServiceImpl<SendDao, SendInformationEntity> implements SendService {

    @Resource
    public SendDao sendDao;

    /**
     * 查询当天的寄件量
     */
    @Override
    public int selectDaySendNumber() {
        // 计算当天的时间范围
        String beginDate = DateUtil.beginOfDay(DateUtil.date()).toString();
        String endDate = DateUtil.endOfDay(DateUtil.date()).toString();
        // 查询当天寄件量
        // 条件构造器
        QueryWrapper<SendInformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(ConstantFinal.FIELD_SEND_CREATE_TIME, beginDate).le(ConstantFinal.FIELD_SEND_CREATE_TIME, endDate);
        // 查库
        return sendDao.selectCount(queryWrapper);
    }

    /**
     * 查询所有数据
     */
    @Override
    public PageInfo<SendInformationEntity> selectSendInformation(String currentPage, String pageSize, String query) {
        QueryWrapper<SendInformationEntity> queryWrapper = new QueryWrapper<>();
        Page<SendInformationEntity> page = PageHelper.startPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        if (!query.isEmpty()) {
            // 查询条件构建
            queryWrapper.like(ConstantFinal.FIELD_SEND_NAME, query).or()
                    .like(ConstantFinal.FIELD_SEND_TO_NAME, query).or()
                    .like(ConstantFinal.FIELD_SEND_ADDRESS, query).or()
                    .like(ConstantFinal.FIELD_SEND_TO_MOBILE, query).or()
                    .like(ConstantFinal.FIELD_SEND_CONTENT, query).or()
                    .like(ConstantFinal.FIELD_SEND_ID, query);
        }
        queryWrapper.orderByDesc(ConstantFinal.FIELD_SEND_CREATE_TIME);
        sendDao.selectList(queryWrapper);
        return page.toPageInfo();
    }


}
